package com.buba.customer.controller;

import com.buba.customer.pojo.Customer;
import com.buba.customer.service.CustomerService;
import com.buba.manifest.service.OrderFormService;
import com.buba.system.pojo.Job;
import com.buba.system.pojo.Page;
import com.buba.util.GenerateKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import java.util.*;

@RestController
@RequestMapping("/Customer")
public class CustomerController {
    private CustomerService customerService;
    private OrderFormService orderFormService;

    /**
     * 查询客户
     * @param map
     * @return
     */
    @RequestMapping("/selCustomerTable")
    public Page selCustomerTable(@RequestParam Map<String, Object> map){
        Page<Customer> page = new Page<>(Integer.parseInt(map.get("pageNow").toString()));
        map.put("page", page);
        String CompanyName = map.get("CompanyName").toString();
        String Phone = map.get("Phone").toString();
        List<Customer> list = null;
        // 没有条件查询
        if ("".equals(CompanyName) && "".equals(Phone)){
        //查询菜单总条数
        page.computePageTotal(customerService.selCustomerCount());
        //查询所有客户
        list = customerService.selCustomerTable(page);
        }else { // 携带查询条件
            page.computePageTotal(customerService.selCount(map));
            list = customerService.selAllCustomer(map);
        }
        //把list装入page类
        page.setList(list);
        return page;
    }


    /**
     * 删除客户
     * @param
     * @return
     */
    @RequestMapping("/delCustomerAll")
    public Map<String,Object> delCustomerAll(String ids){
        Map<String,Object> map = new HashMap<>();
        String[] split = ids.split(",");
        List<String> list = Arrays.asList(split);
        Integer delOrderForm = orderFormService.delOrderFormById(list);
        Integer delCustomer = customerService.delCustomerAll(list);
        if (delCustomer != null){
            map.put("message","删除"+delCustomer+"条数据");
        }else{
            map.put("message","删除失败");
        }
        return map;

    }

    /**
     * 添加客户信息
     * @RequestParam 相当于Servlet中request.getParameter()
     * @return
     */
    @RequestMapping("/addCustomer")
    public Map<String,Object> addCustomer(@RequestParam Map<String, Object> map){
        //指定格式开头加上当前的事件作为id 调用时间戳类
        String customerNo = GenerateKey.getKey("C");
        map.put("customerNo",customerNo);
        //1、添加客户信息 返回添加的条数
        Integer count = customerService.addCustomer(map);
        if(count>0){
            map.put("message","添加成功");
        }else {
            map.put("message","添加失败");
        }
        return map;
    }

    /**
     * 修改客户信息
     * @param customer
     * @return
     */
    @RequestMapping("/updCustomer")
    public Map<String, Object> updCustomer(Customer customer) {
        Map<String, Object> map = new HashMap<>();
        map.put("customer",customer);
        Integer updCount = customerService.updCustomer(map);
        return map;
    }

    /**
     * 查询id
     * @return
     */
    @RequestMapping("/selCustomerCustomerNo")
    public Map<String, Object> selCustomerCustomerNo(){
        Map<String, Object> map = new HashMap<>();
        List<Customer> list = customerService.selCustomerCustomerNo(map);
        map.put("list",list);
        return map;
    }



    @Autowired
    public void setOrderFormService(OrderFormService orderFormService) {
        this.orderFormService = orderFormService;
    }

    @Autowired
    public void setCustomerService(CustomerService customerService) {
        this.customerService = customerService;
    }
}

